<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-dept-search">
				<el-form :inline="true" :model="tableData.param" ref="queryRef" size="default">
					<el-form-item label="部门名称" prop="deptName">
						<el-input v-model="tableData.param.deptName" placeholder="请输入部门名称" clearable style="width: 180px" />
					</el-form-item>
					<el-form-item label="状态" prop="status">
						<el-select placeholder="请选择状态" v-model="tableData.param.status" clearable>
							<el-option label="启用" value="1" />
							<el-option label="禁用" value="0" />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button size="default" type="primary" class="ml10" @click="deptList">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" @click="resetQuery(queryRef)">
							<el-icon>
								<ele-Refresh />
							</el-icon>
							重置
						</el-button>
						<el-button size="default" type="success" class="ml10" @click="onOpenAddDept" v-auth="'api/v1/system/dept/add'">
							<el-icon>
								<ele-FolderAdd />
							</el-icon>
							新增部门
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table
				:data="tableData.data"
				style="width: 100%"
				row-key="deptId"
				default-expand-all
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
				stripe
				size="default"
			>
				<el-table-column prop="deptName" label="部门名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="部门状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="orderNum" label="排序" show-overflow-tooltip></el-table-column>
				<el-table-column label="更新时间" align="center" prop="updateTime" width="180" :formatter="formatter" />
				<el-table-column label="创建时间" align="center" prop="createTime" width="180" :formatter="formatter" />
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenAddDept(scope.$index, scope.row)" v-auth="'api/v1/system/dept/add'"
							>新增
						</el-button>
						<el-button size="small" text type="primary" @click="onOpenEditDept(scope.$index, scope.row)" v-auth="'api/v1/system/dept/edit'"
							>修改
						</el-button>
						<el-button
							size="small"
							text
							type="primary"
							@click="onTabelRowDel(scope.row)"
							v-auth="'api/v1/system/dept/delete'"
							v-if="scope.$index !== 0"
							>删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<EditDept ref="editDeptRef" @deptList="deptList" />
	</div>
</template>

<script lang="ts">
import { defineComponent, getCurrentInstance, onMounted, reactive, ref, toRefs } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import EditDept from '/@/views/system/dept/component/editDept.vue';
import { deleteDept, getDeptList } from '/@/api/system/dept';

// 定义接口来定义对象的类型
interface TableDataRow {
	deptId: number;
	parentId: number;
	deptName: string;
	status: number;
	orderNum: number;
	createTime: string;
	updateTime: string;
	children?: TableDataRow[];
}

interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
			deptName: string;
			status: string;
		};
	};
}

export default defineComponent({
	name: 'systemDeptList',
	components: { EditDept },
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const editDeptRef = ref();
		const queryRef = ref();
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
					deptName: '',
					status: '',
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			deptList();
		};
		const deptList = () => {
			getDeptList(state.tableData.param).then((res: any) => {
				state.tableData.data = res.data.deptTree;
			});
		};
		// 打开新增弹窗
		const onOpenAddDept = (index: number, row?: TableDataRow) => {
			editDeptRef.value.openDialog(index, row?.deptId);
		};
		// 打开编辑弹窗
		const onOpenEditDept = (index: number, row: TableDataRow) => {
			editDeptRef.value.openDialog(index, row);
		};
		// 删除当前行
		const onTabelRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除：${row.deptName}, 是否继续`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteDept(row.deptId).then(() => {
						ElMessage.success('删除成功');
						deptList();
					});
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			deptList();
		};
		// 格式化时间
		const formatter = (row: any, column: any, cellValue: any) => {
			return proxy.moment.unix(cellValue).format('YYYY-MM-DD HH:mm:ss');
		};
		return {
			queryRef,
			editDeptRef,
			deptList,
			resetQuery,
			onOpenAddDept,
			onOpenEditDept,
			onTabelRowDel,
			formatter,
			...toRefs(state),
		};
	},
});
</script>
<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;

		.el-table {
			flex: 1;
		}
	}
}
</style>
